package com.camtc.camcenter.custom;

import com.alibaba.fastjson.JSONObject;
import com.camtc.camcenter.enums.CustomErrorInfo;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

import static com.camtc.camcenter.custom.ResultStatusCode.*;

/**
 * http返回结果
 *
 * @author
 * @date
 */

@Data
@NoArgsConstructor
public class CamHttpResult implements Serializable {

    private Boolean success;
    private String status;
    private String msg;
    private Object data;


    /**
     * 返回码
     *
     * @param resultStatusCode
     */
    public CamHttpResult(ResultStatusCode resultStatusCode) {
        this.success = resultStatusCode.getSuccess();
        this.msg = resultStatusCode.getMsg();
        this.status = resultStatusCode.getCode();
    }

    /**
     * 自定义返回
     *
     * @param status
     * @param msg
     */
    public CamHttpResult(String status, String msg, Boolean success) {
        this.success = success;
        this.status = status;
        this.msg = msg;
    }

    /**
     * 普通异常
     *
     * @return
     */
    public static CamHttpResult failure() {
        return new CamHttpResult(NG);
    }

    /**
     * 自定义异常1
     *
     * @param result
     * @return
     */
    public static CamHttpResult failure(ResultStatusCode result) {
        CamHttpResult r = new CamHttpResult(result);
        return r;
    }

    /**
     * 自定义异常2
     *
     * @param status
     * @param msg
     * @return
     */
    public static CamHttpResult failure(String status, String msg) {
        CamHttpResult r = new CamHttpResult(status, msg, Boolean.FALSE);
        return r;
    }

    /**
     * 自定义异常3
     *
     * @param errorInfo
     * @return
     */
    public static CamHttpResult failure(CustomErrorInfo errorInfo) {
        CamHttpResult r = new CamHttpResult(errorInfo.getCode(), errorInfo.getDescription(), Boolean.FALSE);
        return r;
    }

    /**
     * 正确返回（数据）
     *
     * @param data
     * @return
     */
    public static CamHttpResult ok(Object data) {
        CamHttpResult r = new CamHttpResult(OK);
        r.setData(data);
        return r;
    }

    /**
     * 正确返回（信息）
     *
     * @param msg
     * @return
     */
    public static CamHttpResult ok(String msg) {
        CamHttpResult r = new CamHttpResult(OK);
        r.setMsg(msg);
        return r;
    }

    /**
     * 正确返回（信息，数据）
     *
     * @param msg
     * @param data
     * @return
     */
    public static CamHttpResult ok(String msg, Object data) {
        CamHttpResult r = new CamHttpResult(OK);
        r.setMsg(msg);
        r.setData(data);
        return r;
    }

    @Override
    public String toString() {
        return JSONObject.toJSONString(this);
    }

}
